/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Prin.java
 *
 * Created on 27-nov-2011, 15:17:24
 */
package Principal.existencias;

import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import librerias.existencias.*;

/**
 *
 * static ListaDobleConOrden ldco = new ListaDobleConOrden();
 * @author JONATHAN
 */
public class Existencias extends javax.swing.JFrame {

    double promedio = 0;
    public DefaultTableModel miModelo;
    public String[][] data = {};
    public int elementos_periodo;
    Productos escal = new Productos();
    static ListaDobleConOrden_Existencia ldco = new ListaDobleConOrden_Existencia();

    JTable getMiTabla() {
        return Tabla;
    }

    public DefaultTableModel getMiModelo() {
        return miModelo;
    }

    /** Creates new form Prin */
    public Existencias() {
        initComponents();
        elementos.setValue(1);
        miModelo = new DefaultTableModel(data, new String[]{""}) {
        };
        Tabla.setModel(miModelo);
        jScrollPane1.setViewportView(Tabla);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        Tabla = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        elementos = new javax.swing.JSpinner();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Existencias o inventarios");

        Tabla.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(Tabla);

        jButton1.setText("Generar Tabla");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setText("Ingrese número de productos");

        jMenu1.setText("Ejecucion");

        jMenuItem1.setText("Procesar");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuItem2.setText("Salir");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem2);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(33, 33, 33)
                        .addComponent(elementos, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton1))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 425, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(elementos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 33, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 268, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    elementos_periodo = Integer.parseInt(elementos.getValue().toString());
    if (elementos_periodo != 0) {
        Object[] titulos = new Object[3];
        titulos[0] = "Producto";
        titulos[1] = "Requerimiento anual";
        titulos[2] = "Precio del articulo";
        getMiModelo().setColumnIdentifiers(titulos);
        getMiModelo().setColumnCount(3);
        getMiModelo().setRowCount(elementos_periodo);

        for (int i = 0; i < getMiModelo().getRowCount(); i++) {
            //getMiTabla().setValueAt(i + 1, i, 0);
            getMiTabla().setValueAt(((char) (i + 65) + ""), i, 0);
        }

    }
}//GEN-LAST:event_jButton1ActionPerformed

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
// TODO add your handling code here:
    try {
        ldco.setInicio(null);
        AsignarArreglo();
        promedio = 0;
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, "Los campos no admiten vacio");
    }
}//GEN-LAST:event_jMenuItem1ActionPerformed

private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
dispose();
}//GEN-LAST:event_jMenuItem2ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Existencias.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Existencias.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Existencias.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Existencias.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Existencias().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable Tabla;
    private javax.swing.JSpinner elementos;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

    private void AsignarArreglo() {
        ASIGNAR_DATOS_EN_ARREGLO();
        GENERACION_VENTANA();
    }

    private void ASIGNAR_DATOS_EN_ARREGLO() {

        for (int j = 0; j < getMiModelo().getRowCount(); j++) {
            Object[] fila = new Object[3];
            for (int k = 0; k < getMiModelo().getColumnCount(); k++) {
                fila[k] = getMiModelo().getValueAt(j, k);
                //System.out.println("..." + fila[k]);
            }
            promedio += Double.parseDouble(fila[1].toString()) * Double.parseDouble(fila[2].toString());
            Ingresa_Datos_ESTRUCTURA_DINAMICA(fila[0], fila[1], fila[2], Double.parseDouble(fila[1].toString()) * Double.parseDouble(fila[2].toString()));
            ldco.agrega(escal);
        }
    }

    private void Ingresa_Datos_ESTRUCTURA_DINAMICA(Object Articulo, Object Req_anual, Object Precio, double multi) {
        escal = new Productos();
        escal.setPrecio(Double.parseDouble(Precio.toString()));
        escal.setProducto(Articulo.toString());
        escal.setReq_Anual(Double.parseDouble(Req_anual.toString()));
        escal.setmultiplo(multi);
    }

    private void GENERACION_VENTANA() {
        Resultados_Existencias objeto = new Resultados_Existencias(ldco, promedio);
        objeto.show();
    }

    
}
